/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import model.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class HoSoDao {
    private static Session session;
    
    public HoSoDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertHoSo
     */
    public boolean insertHoSo(String maNhanVien, String maChucVu, String trinhDoVanHoa, String phongBan, int trangThai, int maLuong, String hodem, String ten, String gioiTinh, String chungMinhThu, String queQuan, String diaChiHienTai, String diaChiLienHe, String danToc, String tonGiao, String tenTruongDaoTao, String heDaoTao, String nghanhDaoTao, String namTotNghiep, String ngoaiNgu, String trinhDoNgoaiNgu, byte[] anh, String ghiChu,Integer soBaoHiem, Date ngayCapSo, Integer luongBaoHiem,Date ngayThayDoi){
        try {
            session.beginTransaction();
            
            ChucVu chucVu = (ChucVu)session.get(ChucVu.class, maChucVu);
            TrinhDoVanHoa trinhDo = (TrinhDoVanHoa)session.get(TrinhDoVanHoa.class, trinhDoVanHoa);
            PhongBan phong = (PhongBan)session.get(PhongBan.class, phongBan);
            TrangThai trangThai1 = (TrangThai)session.get(TrangThai.class, trangThai);
            Luong luong = (Luong)session.get(Luong.class, maLuong);
            
            HoSo hoSo = new HoSo(maNhanVien, chucVu, trinhDo, phong, trangThai1, luong, hodem, ten, gioiTinh, chungMinhThu, queQuan, diaChiHienTai, diaChiLienHe, danToc, tonGiao, tenTruongDaoTao, heDaoTao, nghanhDaoTao, namTotNghiep, ngoaiNgu, trinhDoNgoaiNgu, anh, ghiChu);
            BaoHiem baoHiem = new BaoHiem(hoSo, soBaoHiem, ngayCapSo, luongBaoHiem, ngayThayDoi);
            hoSo.setBaoHiems(baoHiem);
            
            chucVu.getHoSos().add(hoSo);
            luong.getHoSos().add(hoSo);
            trinhDo.getHoSos().add(hoSo);
            trangThai1.getHoSos().add(hoSo);
            phong.getHoSos().add(hoSo);
            
            session.save(hoSo);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updateHoSo
     */
    public boolean updateHoSo(String maNhanVien, String maChucVu, String trinhDoVanHoa, String phongBan, int trangThai, int maLuong, String hodem, String ten, String gioiTinh, String chungMinhThu, String queQuan, String diaChiHienTai, String diaChiLienHe, String danToc, String tonGiao, String tenTruongDaoTao, String heDaoTao, String nghanhDaoTao, String namTotNghiep, String ngoaiNgu, String trinhDoNgoaiNgu, byte[] anh, String ghiChu,Integer soBaoHiem, Date ngayCapSo, Integer luongBaoHiem,Date ngayThayDoi){
        try {
            session.beginTransaction();
            
            ChucVu chucVu = (ChucVu)session.get(ChucVu.class, maChucVu);
            TrinhDoVanHoa trinhDo = (TrinhDoVanHoa)session.get(TrinhDoVanHoa.class, trinhDoVanHoa);
            PhongBan phong = (PhongBan)session.get(PhongBan.class, phongBan);
            TrangThai trangThai1 = (TrangThai)session.get(TrangThai.class, trangThai);
            Luong luong = (Luong)session.get(Luong.class, maLuong);
            
            HoSo hoSo = (HoSo)session.get(HoSo.class, maNhanVien);
            hoSo.setAnh(anh);
            hoSo.setChungMinhThu(chungMinhThu);
            hoSo.setDanToc(danToc);
            hoSo.setDiaChiHienTai(diaChiHienTai);
            hoSo.setDiaChiLienHe(diaChiLienHe);
            hoSo.setGhiChu(ghiChu);
            hoSo.setGioiTinh(gioiTinh);
            hoSo.setHeDaoTao(heDaoTao);
            hoSo.setHodem(hodem);
            hoSo.setNamTotNghiep(namTotNghiep);
            hoSo.setNghanhDaoTao(nghanhDaoTao);
            hoSo.setNgoaiNgu(ngoaiNgu);
            hoSo.setQueQuan(queQuan);
            hoSo.setTen(ten);
            hoSo.setTenTruongDaoTao(tenTruongDaoTao);
            hoSo.setTonGiao(tonGiao);
            hoSo.setTrinhDoNgoaiNgu(trinhDoNgoaiNgu);
            
            hoSo.setChucVu(chucVu);
            hoSo.setLuong(luong);
            hoSo.setTrinhDoVanHoa(trinhDo);
            hoSo.setTrangThai(trangThai1);
            hoSo.setPhongBan(phong);
            
            chucVu.getHoSos().add(hoSo);
            luong.getHoSos().add(hoSo);
            trinhDo.getHoSos().add(hoSo);
            trangThai1.getHoSos().add(hoSo);
            phong.getHoSos().add(hoSo);
            
            hoSo.getBaoHiems().setLuongBaoHiem(luongBaoHiem);
            hoSo.getBaoHiems().setNgayCapSo(ngayCapSo);
            hoSo.getBaoHiems().setNgayThayDoi(ngayThayDoi);
            hoSo.getBaoHiems().setSoBaoHiem(soBaoHiem);
            
            session.update(hoSo);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deleteHoSo
     */
    public boolean deleteHoSo(String maNhanVien){
        try {
            session.beginTransaction();
            
            HoSo hoSo = (HoSo)session.get(HoSo.class, maNhanVien);
            
            session.delete(hoSo);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
}
